#include <assert.h>
#include <Timer.hpp>
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
using namespace std;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#ifdef __MAC_OSX__
void clock_gettime( const uint32_t unused, timespec * tsp ) {
    tsp->tv_nsec = mach_absolute_time();
}
void Timer::TimeDiff( void ) {

    static mach_timebase_info_data_t info = {0,0};
    if( info.denom == 0 ) {
        mach_timebase_info( &info );
        assert( info.numer );
        assert( info.denom );
        assert( info.numer % info.denom == 0ULL );
    }
    uint64_t diff  = tstop.tv_nsec - tstart.tv_nsec;
    uint64_t dnano = diff * ( info.numer / info.denom );
    dt = 1e-9 * double( dnano );
}
#else
void Timer::TimeDiff( void ) {
    uint64_t dsec;
    uint64_t dnsec;

    if( ( tstop.tv_nsec - tstart.tv_nsec ) < 0 ) {
        dsec  = tstop.tv_sec - tstart.tv_sec - 1ULL;
        dnsec = 1000000000ULL + tstop.tv_nsec - tstart.tv_nsec;
    }
    else {
        dsec  = tstop.tv_sec  - tstart.tv_sec;
        dnsec = tstop.tv_nsec - tstart.tv_nsec;
    }

    dt = double( dsec ) + 1e-9 * double( dnsec );
}
#endif

